﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace DamageCalculator.Classes
{
    public class StaticFunctions
    {
        #region Settings Helpers
        public static void WriteXML(DamageCalculator.Classes.GameSettings GS)
        {
            if (GS.GameName == "" || GS.GameName == null)
                throw new Exception("Game Name must be set to save");

            XmlSerializer X = new XmlSerializer(typeof(DamageCalculator.Classes.GameSettings));
            String MydocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Damage Calculator";
            if(!Directory.Exists(MydocsPath + @"\Game Settings Files"))
                Directory.CreateDirectory(MydocsPath +  @"\Game Settings Files");

            FileStream S = new FileStream(MydocsPath + @"\Game Settings Files\" + GS.GameName + ".xml", FileMode.Create, FileAccess.Write);

            X.Serialize(S, GS);
            S.Close();
        }

        public static DamageCalculator.Classes.GameSettings ReadXML(String FileName)
        {
            if (!File.Exists(FileName))
                throw new Exception("Settings file not found.");

            XmlSerializer X = new XmlSerializer(typeof(DamageCalculator.Classes.GameSettings));
            FileStream S = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            Classes.GameSettings toreturn = X.Deserialize(S) as DamageCalculator.Classes.GameSettings;
            S.Close();

            return toreturn;
        }

        public static List<GameSettings> GetGamesSettingsList()
        {
            List<GameSettings> toReturn = new List<GameSettings>();
            String MydocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Damage Calculator";
            if (!Directory.Exists(MydocsPath + @"\Game Settings Files"))
                Directory.CreateDirectory(MydocsPath + @"\Game Settings Files");

            String[] files = Directory.GetFiles(MydocsPath + @"\Game Settings Files");

            foreach (String S in files)
            {
                try
                {
                    toReturn.Add(ReadXML(S));
                }
                catch { }
            }

            return toReturn;
        }
        #endregion

        public static long GenerateEncounterID()
        {
            DateTime tempDt = DateTime.Now;

            String IdString = tempDt.Year.ToString("0000") + tempDt.Month.ToString("00") + tempDt.Day.ToString("00") + tempDt.Hour.ToString("00") + tempDt.Minute.ToString("00") + tempDt.Second.ToString("00");

            return long.Parse(IdString);
        }

        public static void LVICopy(ListViewItem toCopy, ListViewItem Copyto)
        {
            Copyto.Text = toCopy.Text;
            for (int i = 1; i < toCopy.SubItems.Count; i++)
            {
                if(Copyto.SubItems[i].Text != toCopy.SubItems[i].Text)
                    Copyto.SubItems[i].Text = toCopy.SubItems[i].Text;
            }
        }
    }
}
